<?php
$IP = dirname( __FILE__ );
define('MASTER_HOST', 'localhost');
define('MASTER_USER', 'enwn');
define('MASTER_PASS', 'wikinewzrulez');
define('MASTER_DB', 'enwn');
define('DEBUG_ON', TRUE);

// ********************************
// SQL Connection Information
// ********************************
// -- Master Connection (r/w)
$GLOBALS['con'] = mysqli_connect(MASTER_HOST, MASTER_USER, MASTER_PASS) or die("Cannot Connect to Master");
mysqli_select_db($GLOBALS['con'], MASTER_DB);
mysqli_autocommit($GLOBALS['con'], TRUE);

// ********************************
// Memcache Connection
// ********************************
//$GLOBALS['mem'] = memcache_connect('10.31.1.107', 11211);

// ********************************
// Global Variables
// ********************************
$GLOBALS['PrjName']	= "EnWn.net";
$GLOBALS['SiteURL']     = 'http://enwn.net';				// no trailing slash
$GLOBALS['RelURL']	= '/';						// Relative URL to root
$GLOBALS['email']	= 'nobody@enwn.net';				// Email "From" address

// ********************************
// Required Files
// Note:
// Session call must be first, for cookie headers
// ********************************

$propercall = true;

// ********************************
// Start generic functions below here:
// ********************************


function randCode($len){
	$tmp = substr( sha1(time(true) . mt_rand(1,500)), mt_rand(0,10), $len);
	// uppercase characters with 50% chance
	$code = '';
	for ($i = 0; $i < strlen($tmp) ; $i++) {
		$val = substr($tmp, $i, 1);
	
		if (!is_numeric($val) && (rand() % 2 == 0)) {
			$val = strtoupper($val);
		}
		
		$code .= $val;
	}
	
	return $code;
}

function clean($in){			// -- Clean submitted data
	$out = addslashes($in);
	$out = trim($out);
	return $out;
}

function createURL($url, $len = 5, $domain = "0"){
        $url = clean(trim($url));
        //$title = mysqli_real_escape_string($GLOBALS['con'], $title);
        if(!preg_match("/^(ht|f)tp(s*):\/\/(.*)\.(.*)/", $url, $out)){
                print "ERROR-NOTVALID ||$url||\n";
                die;
        }

	$host = parse_url($url, PHP_URL_HOST);
	$tmp = array_reverse(explode(".", $host));
	$host = $tmp[1].".".$tmp[0];
	if(!$GLOBALS['wloverride']){
		$res = mysqli_query($GLOBALS['con'], "SELECT COUNT(`id`) as c FROM `whitelist` WHERE `domain` = '$host'");
		$row = mysqli_fetch_array($res);
		if(!$row['c']){
			die("ERROR-NOTALLOWED");
		}
	}

        $res = mysqli_query($GLOBALS['con'], "SELECT `code` FROM `links` WHERE `hash`=sha1('$url')");
        if( $row = mysqli_fetch_array($res) ){
                // It already exists in the system
		return $row['code'];
        } else {
                //Doesn't exist in the system
                $count=0;
                do {
                        $count++;
                        $code = randCode($len);
                        mysqli_query($GLOBALS['con'], "INSERT INTO `links` (`code`, `hash`, `url`, `domain`) VALUES ('$code', sha1('$url'), '$url', '$domain')");
                        if($count == 3){$len++;$count == 0;}
                        if($len >= 10){die("Massive Error");}
                } while( mysqli_affected_rows($GLOBALS['con']) != 1);
                //It's been created.
                return $code;
        }
}

function logEntry($event,$relatedid,$message,$message2=null){
	if(DEBUG_ON){ print "EVENT: $event -- RELATEID: $relatedid -- MESSAGE: $message\n"; }
	$message2 = mysqli_real_escape_string($GLOBALS['con'], $message2);
	mysqli_query($GLOBALS['con'], "INSERT INTO `logs` (`event`, `relatedid`, `message`, `message2`) VALUES ('$event','$relatedid','$message', '$message2')");
}
